﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RuEn
{
    /// <summary>
    /// Attribute class for Comparer Rules. Compare one value with another using the defined Criteria
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
    public class ComparerBoolRule : CompareRuleBase
    {
        private string _errorMessage = "Bool Comparions failed";
        private bool _comparer;
        private ComparerEnum _operation;

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="comparer">Value to compare with</param>
        /// <param name="operation">Comparision Criteria</param>
        public ComparerBoolRule(bool comparer, ComparerEnum operation)
        {
            _comparer = comparer;
            _operation = operation;
        }

        /// <summary>
        /// Overloaded Constructor
        /// </summary>
        /// <param name="comparer">Value to compare with</param>
        /// <param name="ErrorMessage">Custom Error Message</param>
        /// <param name="operation">Comparision Criteria</param>
        public ComparerBoolRule(bool comparer, string ErrorMessage, ComparerEnum operation)
        {
            _comparer = comparer;
            _operation = operation;
            _errorMessage = ErrorMessage;
        }

        /// <summary>
        /// Public property for Custom Error Message
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        /// <summary>
        /// Public Property for value to compare with
        /// </summary>
        public bool Comparer
        {
            get { return _comparer; }
            set { _comparer = value; }
        }

        /// <summary>
        /// Public Property for Compare criteria
        /// </summary>
        public ComparerEnum Operation
        {
            get { return _operation; }
            set { _operation = value; }
        }

        public bool Validate(bool value, bool Comparer, string ErrorMessage, ComparerEnum operation)
        {
            switch (operation)
            {
                case ComparerEnum.Equel:
                    if (value != Comparer)
                    {
                        BrokenRules.Add(ErrorMessage);
                        return false;
                    }
                    break;
                case ComparerEnum.NotEquel:
                    if (value == Comparer)
                    {
                        BrokenRules.Add(ErrorMessage);
                        return false;
                    }
                    break;
                default:
                    if (value != Comparer)
                    {
                        BrokenRules.Add(ErrorMessage);
                        return false;
                    }
                    break;
            }
            return true;
        }
    }


}
